package com.atguigu.gulimall.member.service.impl;
import com.atguigu.common.utils.R;
import com.atguigu.gulimall.member.dao.MemberLevelDao;
import com.atguigu.gulimall.member.entity.MemberLevelEntity;
import com.atguigu.gulimall.member.excepttion.PhoneExistException;
import com.atguigu.gulimall.member.excepttion.UsernameExistException;
import com.atguigu.gulimall.member.vo.MemberLoginVo;
import com.atguigu.gulimall.member.vo.MemberRegistVo;
import org.aspectj.lang.annotation.AfterReturning;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.atguigu.common.utils.PageUtils;
import com.atguigu.common.utils.Query;
import com.atguigu.gulimall.member.dao.MemberDao;
import com.atguigu.gulimall.member.entity.MemberEntity;
import com.atguigu.gulimall.member.service.MemberService;
@Service("memberService")
public class MemberServiceImpl extends ServiceImpl<MemberDao, MemberEntity> implements MemberService {
    @Autowired
    private MemberLevelDao memberLevelDao;
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<MemberEntity> page = this.page(
                new Query<MemberEntity>().getPage(params),
                new QueryWrapper<MemberEntity>()
        );
        return new PageUtils(page);
    }
    @Override
    public void regist(MemberRegistVo vo) {
        MemberDao baseMapper1 = this.baseMapper;
        MemberEntity memberEntity = new MemberEntity();
        MemberLevelEntity memberLevelEntity= memberLevelDao.getDefaultLevel();
        // 设置默认等级
        memberEntity.setLevelId(memberLevelEntity.getId());
        //在保存用户之前,应该检查 他的  手机号   和  用户名   的   唯一性
   checkPhoneUnique(vo.getPhone());
 checkUsernameUnique(vo.getUserName());
        memberEntity.setMobile(vo.getPhone());
        memberEntity.setUsername(vo.getUserName());
//   设置密码,  但是  要 把  密码 加密
        BCryptPasswordEncoder bCryptPasswordEncoder = new BCryptPasswordEncoder();
        String encode = bCryptPasswordEncoder.encode(vo.getPassword());
        memberEntity.setPassword(encode);
        baseMapper1.insert(memberEntity);
    }
    @Override
    public void checkUsernameUnique(String username)  throws  UsernameExistException{
        MemberDao baseMapper1 = this.baseMapper;
        Integer i = baseMapper1.selectCount(new QueryWrapper<MemberEntity>().eq(("username"), username));
        if(i>0){
        UsernameExistException usernameExistException = new UsernameExistException();
        throw   usernameExistException;
        }
    }
    @Override
    public void checkPhoneUnique(String phone) throws  PhoneExistException{
        MemberDao baseMapper1 = this.baseMapper;
        Integer i = baseMapper1.selectCount(new QueryWrapper<MemberEntity>().eq("mobile", phone));
        if(i>0){
            PhoneExistException phoneExistException = new PhoneExistException();
            throw phoneExistException;
        }
    }
    @Override
    public MemberEntity login(MemberLoginVo vo) {
        String loginAccount = vo.getLoginAccount();
        MemberDao baseMapper1 = this.baseMapper;
        QueryWrapper<MemberEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("username",loginAccount);
        wrapper.or().eq("mobile",loginAccount);
        MemberEntity memberEntity = baseMapper1.selectOne(wrapper);
        if(memberEntity==null){
            return null;
        }else{
            String passwordFromDB = memberEntity.getPassword();
            BCryptPasswordEncoder bCryptPasswordEncoder = new BCryptPasswordEncoder();
            boolean matches = bCryptPasswordEncoder.matches(vo.getPassword(), passwordFromDB);
            if(matches){
                return  memberEntity;
            }else{
                return  null;
            }
        }
    }
}